package cn.znzz.cjm.org.service.impl;

import cn.znzz.cjm.basic.exception.BusinessException;
import cn.znzz.cjm.basic.service.impl.BaseServiceImpl;
import cn.znzz.cjm.org.domain.Employee;
import cn.znzz.cjm.org.domain.Shop;
import cn.znzz.cjm.org.mapper.EmployeeMapper;
import cn.znzz.cjm.org.mapper.ShopMapper;
import cn.znzz.cjm.org.service.IEmployeeService;
import cn.znzz.cjm.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务层实现
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        //参数不为空
        //两次密码是否一致
        Employee admin = shop.getAdmin();
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致！！");
        }
        //是否账号已经注册，通过三个username，phone，email
        Employee employee=employeeMapper.loadByUser(admin.getUsername());
        if(employee !=null){
            throw new BusinessException("账号存在，直接登录");
        }
        //提示重读，直接登录
        //保存员工信息，返回自增的id
        //employeeMapper.save(admin);保存员工，级联保存logininfo
        employeeService.add(admin);
        //设置shop里面的admin的id，保存shop，返回shop的自增id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //设置employee中的shop_id，修改employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    //基本的crud实现被抽取
}













